// require('../css/enterprise.scss')
define(['../userModule'], function(module) {
    module.controller("userDetailCtrl", ['$scope', '$rootScope', '$state', '$stateParams', '$sessionStorage', '$timeout', 'httpService', 'ENV', 'growl', 'Upload', 'ngDialog', 'Md5', function($scope, $rootScope, $state, $stateParams, $sessionStorage, $timeout, httpService, ENV, growl, Upload, ngDialog, Md5) {
        'use strict';
        $scope.userId = $stateParams.userId;
        console.log($stateParams);
        httpService.linkHttp({
            url: ENV.userDetail
        }, {userId: $scope.userId}).then(function(res) {
            // 判断请求成功
            var result = res.data;
            if (result.code == "000000") {
                $scope.adduserInfo = result.data.userDetail;
			}
        });


        $scope.detailUser = function(valid) {

            if (valid) {
                // 禁用按钮
                $scope.disableFlag = true;

                // $scope.adduserInfo.password = Md5.hex_md5($scope.password);

                var data = {};
                data.mobileNo = $scope.adduserInfo.mobile_no;
                data.name = $scope.adduserInfo.user_name;
                data.sex = $scope.adduserInfo.sex;
                data.idType = $scope.adduserInfo.id_type;
                $scope.adduserInfo.birthday ? data.birthday = $scope.adduserInfo.birthday : '';
                data.idNo = $scope.adduserInfo.id_no;
                data.email = $scope.adduserInfo.email;
                data.contactNo = $scope.adduserInfo.contact_no;
                data.userId = $scope.userId;
                console.log(data);

                // 转为JSON串
                // data = angular.toJson($scope.adduserInfo, true);

                httpService.linkHttp({
                    url: ENV.userEdit
                },data).then(function(res) {
                    // 判断请求成功
                    var result = res.data;
                    if (result.code == "000000") {
                        // 重置表单
                        $scope.adduserInfo = {};
                        $scope.password = '';
                        $scope.date = {};
                        $timeout(function () {
                            $state.go('main.user');
                        }, 1000);
                        growl.addSuccessMessage("编辑成功,1秒后将跳转到用户列表页");
                    } else {
                        var errText;
                        result.errDesc ? errText = result.errDesc : errText = '编辑失败, 请稍后再尝试';
                        growl.addErrorMessage(errText);
                    }
                    $scope.disableFlag = false;
                });
            } else {
                $scope.chechErrorForm($scope.addUserForm.$error);
            }
        }


    }])
});
